package kd.fi.gl.util.wchn;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.excel.util.ObjectUtils;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.db.DB;
import kd.bos.db.DBRoute;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.dataentity.DynamicObjectResultSetCallback;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.org.OrgUnitServiceHelper;
import kd.bos.servicehelper.org.OrgViewType;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @param: null
 * @description: 健康发布接口接收MDG客户、供应商、行名行号信息
 * @return:
 * @author: zilongluo
 * @date: 2022/7/5
 */
public class JkServiceImpl implements JkService {
    private static final Log logger = LogFactory.getLog(JkServiceImpl.class);
    //资金前置机地址
    private String URL_RMB_PAYMENT = "";
    //资金用户名
    private String APPUSER = "";
    //资金密码
    private String APPSEC = "";
    private String MKCH = "根据每刻入参,苍穹未匹配到相关单据";

    /**
     * @param: params
     * @description: 获取mdg客户信息接口
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author: zilongluo
     * @date: 2021/6/28
     */
    @Override
    public Map<String, Object> doCustomServiceCustomer(Map<String, Object> params) {
        boolean isSuccess = false;
        HashMap<String, Object> apiResult = new HashMap<>();

        List<Map<String, Object>> datas = (List<Map<String, Object>>) params.get("datas");
        if (null != datas && datas.size() > 0) {
            for (Map<String, Object> data : datas) {
                boolean deleteSign = (boolean) data.get("deleteSign");
                if (true == deleteSign) {
                    String number = (String) data.get("number");
                    DynamicObject dynamicObjectDelete = BusinessDataServiceHelper.loadSingle("bd_customer", "id", new QFilter[]{new QFilter("number", QCP.equals, number)});
                    if (null != dynamicObjectDelete) {
                        Object id = dynamicObjectDelete.get("id");
                        Object[] ids = {id};
                        OperationResult operationResult = OperationServiceHelper.executeOperate("disable", "bd_customer", ids, OperateOption.create());
                        boolean success = operationResult.isSuccess();
                        if (true == success) {
                            apiResult.put("errorCode", "1001");
                            apiResult.put("success", true);
                            apiResult.put("message", "数据禁用操作成功!");
                            apiResult.put("data", "1001");
                        } else {
                            apiResult.put("errorCode", "1001");
                            apiResult.put("success", false);
                            apiResult.put("message", "数据禁用操作失败!");
                            apiResult.put("data", "1002");
                        }
                    } else {
                        apiResult.put("message", "苍穹无此数据，无法禁用!");
                    }
                } else {
                    String number = (String) data.get("number");
                    List<Map<String, Object>> createorgs = (List<Map<String, Object>>) data.get("createorg");//创建组织sap编码
                    //mdg传的是sap编码，需要转换一次
                    List<Map<String, Object>> createorg = new ArrayList<>();
                    //将物产集团组织编码存入
                    HashMap<String, Object> objectObjectHashMap = new HashMap<>();
                    objectObjectHashMap.put("number", "M10000692");
                    DynamicObject orgId = QueryServiceHelper.queryOne("bos_org", "id", new QFilter[]{new QFilter("number", QCP.equals, "M10000692")});
                    createorg.add(objectObjectHashMap);
//                    if(null!=createorgs&&createorgs.size()>0){
//                        for (Map<String, Object> stringObjectMap : createorgs) {
////                            String sapnumber = (String) stringObjectMap.get("number");
////                            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bos_org", "id,number", new QFilter[]{new QFilter("zjmi_sapnumber", QCP.equals, sapnumber)});
//                            String sapnumber = (String) stringObjectMap.get("number");
//                            QFilter bukrsQFilter = new QFilter("zjmi_sapnumber", QCP.equals, sapnumber);
//                            DynamicObject[] bos_orgDynamicObjects = BusinessDataServiceHelper.load("bos_org", "id,number,zjmi_sapnumber", new QFilter[]{bukrsQFilter});
//                            DynamicObject dynamicObject = null;
//                            if(!ObjectUtil.isEmpty(bos_orgDynamicObjects)){
//                                for (DynamicObject bos_orgDynamicObject : bos_orgDynamicObjects) {
//                                    Long id = (Long) bos_orgDynamicObject.get("id");
//                                    boolean hasAdminBiz = OrgUnitServiceHelper.checkOrgFunction(id, OrgViewType.OrgUnit);
//                                    if(hasAdminBiz){
//                                        dynamicObject=bos_orgDynamicObject;
////                                        logger.info(number+"供应商分配组织"+dynamicObject.get("number"));
//                                    }
//                                }
//                            }
//                            //获取sap编码对应的苍穹的业务组织编码
//                            if(!ObjectUtils.isEmpty(dynamicObject)){
//                                String orgNumber = (String) dynamicObject.get("number");
//                                Map<String,Object> orgChange=new HashMap<>();
//                                orgChange.put("number",orgNumber);
//                                createorg.add(orgChange);
//                            }
//                        }
//                    }
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_customer", "id,enable", new QFilter[]{new QFilter("number", QCP.equals, number)});
                    if (null != dynamicObject) {
                        Object idDynamicObject = dynamicObject.get("id");
                        DynamicObject dynamicObjectCustomer = BusinessDataServiceHelper.loadSingle(idDynamicObject, "bd_customer");
                        dynamicObjectCustomer.set("masterid", idDynamicObject);

                        if (StringUtils.equals("0", dynamicObject.getString("enable"))) {
                            try {
                                OperationResult operationResult = OperationServiceHelper.executeOperate("enable", "bd_customer", new Object[]{idDynamicObject}, OperateOption.create());
                            } catch (Exception e) {
                                e.printStackTrace();
                                logger.info("数据启用失败" + e.getMessage());

                            }
                        }


                        setCustomerHeader(data, dynamicObjectCustomer);
                        setCustomerBusiness(data, dynamicObjectCustomer);
//                        setentryTicket(data,dynamicObjectCustomer);
                        setBank(data, dynamicObjectCustomer);
                        //setentryAccount(data,dynamicObjectCustomer);//统计会计科目
                        SaveServiceHelper.update(new DynamicObject[]{dynamicObjectCustomer});
                        setCustomerEntryGroupstandard(data, dynamicObjectCustomer);

                        QFilter qFilter = new QFilter("id", QCP.equals, idDynamicObject);
                        DynamicObject dynamicObjects = BusinessDataServiceHelper.loadSingle("bd_customer", "id", new QFilter[]{qFilter});
                        Object id = dynamicObjects.get("id");
                        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
                        DBRoute dbRoute = new DBRoute("sys");
                        String deletesql = "delete from t_bd_customer_u where fdataid=?";
                        Object[] paramDelete = {id};
                        boolean execute1 = DB.execute(dbRoute, deletesql, paramDelete);
                        String deletesqlreg = "delete from T_BD_Customerusereg where fdataid=?";
                        boolean executereg = DB.execute(dbRoute, deletesqlreg, paramDelete);

                        for (Map<String, Object> stringObjectMap : createorg) {
                            Object number1 = stringObjectMap.get("number");
                            QFilter qFilter1 = new QFilter("number", QCP.equals, number1);
                            DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle("bos_org", "id", new QFilter[]{qFilter1});
                            Object id1 = dynamicObject1.get("id");
                            String sql = "insert into t_bd_customer_u(fdataid,fuseorgid) values(?,?)";
                            Object[] param1 = {id, id1};
                            boolean execute = DB.execute(dbRoute, sql, param1);
                            insertCustomerusereg(dynamicObjects, dynamicObject1);
                        }
                        isSuccess = true;
                    } else {
                        DynamicObject customer = BusinessDataServiceHelper.newDynamicObject("bd_customer");
                        this.setCustomerHeader(data, customer);
                        this.setCustomerBusiness(data, customer);
//                        this.setentryTicket(data,customer);
                        this.setBank(data, customer);
                        //this.setentryAccount(data,customer);
                        Object[] save = SaveServiceHelper.save(new DynamicObject[]{customer});
                        if (null != save && save.length > 0) {
                            //因生成凭证需要，需要给masterid赋值
                            DynamicObject dynamicObjectforMaterId = BusinessDataServiceHelper.loadSingle("bd_customer", "id,number,masterid", new QFilter[]{new QFilter("number", QCP.equals, number)});
                            Long id = (Long) dynamicObjectforMaterId.get("id");
                            dynamicObjectforMaterId.set("masterid", id);
                            SaveServiceHelper.save(new DynamicObject[]{dynamicObjectforMaterId});
                        }
                        setCustomerEntryGroupstandard(data, customer);
                        /**
                         * 组织自动分配逻辑
                         * 1.获取客户表单据中数据中的id
                         * 2.获取业务单元表单据数据中的id
                         * 3.将两张单据关联存储
                         */
                        QFilter qFilter = new QFilter("number", QCP.equals, number);
                        DynamicObject dynamicObjects = BusinessDataServiceHelper.loadSingle("bd_customer", "id", new QFilter[]{qFilter});
                        Object id = dynamicObjects.get("id");
                        for (Map<String, Object> stringObjectMap : createorg) {
                            Object number1 = stringObjectMap.get("number");
                            QFilter qFilter1 = new QFilter("number", QCP.equals, number1);
                            DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle("bos_org", "id", new QFilter[]{qFilter1});
                            Object id1 = dynamicObject1.get("id");
                            DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
                            DBRoute dbRoute = new DBRoute("sys");
                            String sql = "insert into t_bd_customer_u(fdataid,fuseorgid) values(?,?)";
                            Object[] param1 = {id, id1};
                            boolean execute = DB.execute(dbRoute, sql, param1);
                            insertCustomerusereg(dynamicObjects, dynamicObject1);//分配表

                        }
                        isSuccess = true;
                    }
                    if (isSuccess) {
                        apiResult.put("errorCode", "1001");
                        apiResult.put("success", true);
                        apiResult.put("message", "操作成功!");
                        apiResult.put("data", "1001");
                    } else {
                        apiResult.put("errorCode", "1002");
                        apiResult.put("success", false);
                        apiResult.put("message", "操作失败!");
                        apiResult.put("data", "1002");
                    }
                }
            }
        }
        return apiResult;
    }

    /**
     * @param: data
     * @param: customer
     * @description: 保存客户单据头信息
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/28
     */
    void setCustomerHeader(Map<String, Object> data, DynamicObject customer) {

        customer.set("number", data.get("number"));//设置编码
        customer.set("zjmi_iswcyy", true);//设置编码
        customer.set("name", data.get("name"));//名称
        customer.set("zjmi_supplier_type", data.get("zjmi_supplier_type"));//设置供应商分类字段
        customer.set("zjmi_business_partber_ide", data.get("zjmi_business_partber_ide"));
        DynamicObject dyorg = BusinessDataServiceHelper.loadSingle("bos_org", "id,number,name",
                new QFilter[]{new QFilter("number", "=", "M10000692")});
        DynamicObject[] bos_orgDynamicObjects = BusinessDataServiceHelper.load("bos_org", "id,number,zjmi_sapnumber", new QFilter[]{new QFilter("number", "=", "M10000692")});
        DynamicObject dynamicObject = new DynamicObject();
        if (!ObjectUtils.isEmpty(bos_orgDynamicObjects)) {
            for (DynamicObject bos_orgDynamicObject : bos_orgDynamicObjects) {
                Long id = (Long) bos_orgDynamicObject.get("id");
                boolean hasAdminBiz = OrgUnitServiceHelper.checkOrgFunction(id, OrgViewType.OrgUnit);
                if (hasAdminBiz) {
                    dynamicObject = bos_orgDynamicObject;
                    customer.set("createorg", dynamicObject);
                }
            }
        }


//        customer.set("createorg",dyorg);//创建组织
        customer.set("zjmi_search_item", data.get("zjmi_search_item"));//搜索项
        customer.set("enable", "1");//默认设置为可用
        //控制策略:1:逐级分配 2:自由分配 5:全局共享 6:管控范围内共享 7:私有
        customer.set("ctrlstrategy", "6");
//        customer.set("ctrlstrategy","1");//为1才能分配查询
        //数据状态:A:暂存 B:已提交 C:已审核
        customer.set("status", "C");
        //客户状态
        DynamicObject dynamicObjectCus = BusinessDataServiceHelper.loadSingle("bd_customerstatus", "id,number", new QFilter[]{new QFilter("number", QCP.equals, "ZSKH")});
        customer.set("customerstatus", dynamicObjectCus);

    }

    /**
     * @param: data
     * @param: customer
     * @description: 客户工商信息
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/28
     */
    void setCustomerBusiness(Map<String, Object> data, DynamicObject customer) {
        Map<String, String> country = (Map<String, String>) data.get("country");
        if (null != country && country.size() > 0) {
            String countrynumber = country.get("number");//国家/地区编号
            DynamicObject countryDy = BusinessDataServiceHelper.loadSingle("bd_country",
                    "number", new QFilter[]{new QFilter("number", "=", countrynumber)});
            customer.set("country", countryDy);
        }
        customer.set("societycreditcode", data.get("societycreditcode"));//统一社会信用代码
        customer.set("tx_register_no", data.get("societycreditcode"));
        customer.set("artificialperson", data.get("artificialperson"));//法人代表
        customer.set("regcapital", data.get("regcapital"));//注册资本（万）
        if (null != data.get("zjmi_supplier_type") && data.get("zjmi_supplier_type").equals("0")) {
            customer.set("idno", data.get("societycreditcode"));//当供应商类型为个人时，需要填写身份证号码
            customer.set("type", "4");//伙伴类型
        } else {
            customer.set("type", "M01");//伙伴类型
        }
        String establishdate = (String) data.get("establishdate");//成立日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(establishdate);
        } catch (ParseException e) {
        }
        customer.set("establishdate", date);//成立日期
        customer.set("bizpartner_phone", data.get("bizpartner_phone"));//联系电话
        customer.set("bizpartner_fax", data.get("bizpartner_fax"));//传真
        customer.set("postal_code", data.get("postal_code"));//电子邮箱
        //行政区划
        Map<String, String> admindivision = (Map<String, String>) data.get("admindivision");
        if (null != admindivision && admindivision.size() > 0) {
            String admindivisionnumber = admindivision.get("number");//国家/地区编号
            DynamicObject admindivisionDy = BusinessDataServiceHelper.loadSingle("bd_admindivision",
                    "id,number,name", new QFilter[]{new QFilter("number", "=", admindivisionnumber)});
            if (null != admindivisionDy) {
                customer.set("admindivision", admindivisionDy.get("id"));
            }
        } else {
            customer.set("admindivision", null);
        }
        customer.set("bizpartner_address", data.get("bizpartner_address"));//详细地址
    }

    /**
     * @param: data 每刻传送的参数
     * @param: customer 新增客户对象
     * @description: 获取开票信息
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/28
     */
    void setentryTicket(Map<String, Object> data, DynamicObject customer) {
        DynamicObjectCollection zjmi_entry_ticket = customer.getDynamicObjectCollection("zjmi_entry_ticket");
        //更新前先将已有的单据体清除
        if (null != zjmi_entry_ticket) {
            zjmi_entry_ticket.clear();
        }
        //获取开票信息
        List<Map<String, Object>> zjmi_entry_tickets = (List<Map<String, Object>>) data.get("zjmi_entry_ticket");
        if (null != zjmi_entry_tickets && zjmi_entry_tickets.size() > 0) {
            for (Map<String, Object> zjmiEntryTicket : zjmi_entry_tickets) {
                //在开票信息单据体中新增一行
                DynamicObject dynamicObjectTicketRow = zjmi_entry_ticket.addNew();
                Map<String, Object> zjmi_ticket_org = (Map<String, Object>) zjmiEntryTicket.get("zjmi_ticket_org");
                if (null != zjmi_ticket_org && zjmi_ticket_org.size() > 0) {
                    String orgnumber = (String) zjmi_ticket_org.get("number");
                    QFilter bukrsQFilter = new QFilter("zjmi_sapnumber", QCP.equals, orgnumber);
                    DynamicObject[] bos_orgDynamicObjects = BusinessDataServiceHelper.load("bos_org", "id,number,zjmi_sapnumber", new QFilter[]{bukrsQFilter});
                    DynamicObject dynamicObject = new DynamicObject();
                    if (!ObjectUtils.isEmpty(bos_orgDynamicObjects)) {
                        for (DynamicObject bos_orgDynamicObject : bos_orgDynamicObjects) {
                            Long id = (Long) bos_orgDynamicObject.get("id");
                            boolean hasAdminBiz = OrgUnitServiceHelper.checkOrgFunction(id, OrgViewType.OrgUnit);
                            if (hasAdminBiz) {
                                dynamicObject = bos_orgDynamicObject;
                                dynamicObjectTicketRow.set("zjmi_ticket_org", dynamicObject);
                            }
                        }
                    }
//


//                    DynamicObject dynamicObjectTicket = BusinessDataServiceHelper.loadSingle("bos_org", "id,number", new QFilter[]{new QFilter("zjmi_sapnumber", QCP.equals, orgnumber)});
//                    dynamicObjectTicketRow.set("zjmi_ticket_org",dynamicObjectTicket);
                }
                String zjmi_mdg_ticket = (String) zjmiEntryTicket.get("zjmi_mdg_ticket");
                dynamicObjectTicketRow.set("zjmi_mdg_ticket", zjmi_mdg_ticket);
            }
        }
    }

    /**
     * @param: data
     * @param: customer
     * @description: 设置银行信息
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/28
     */
    void setBank(Map<String, Object> data, DynamicObject customer) {
        DynamicObjectCollection entry_bank = customer.getDynamicObjectCollection("entry_bank");
        if (null != entry_bank) {
            entry_bank.clear();
        }
        List<Map<String, Object>> entryentityDatas2 = (List<Map<String, Object>>) data.get("entry_bank");
        if (null != entryentityDatas2 && entryentityDatas2.size() > 0) {
            for (Map<String, Object> map : entryentityDatas2) {
                //新增一条银行数据
                DynamicObject dynamicObject = entry_bank.addNew();
                if (null != map && map.size() > 0) {
                    String bankaccount = (String) map.get("bankaccount");//银行账号
                    dynamicObject.set("bankaccount", bankaccount);
                    String accountname = (String) map.get("accountname");//账户名称
                    dynamicObject.set("accountname", accountname);
                    //开户银行
                    Map<String, String> bank = (Map<String, String>) map.get("bank");
                    if (null != bank && bank.size() > 0) {
                        String banknumber = bank.get("number");//开户银行编号
                        DynamicObject dybank = BusinessDataServiceHelper.loadSingle("bd_bebank",
                                "number", new QFilter[]{new QFilter("number", "=", banknumber)});
                        dynamicObject.set("bank", dybank);
                    }
                }
            }
        }
        SaveServiceHelper.save(new DynamicObject[]{customer});

    }


    /**
     * @param: data
     * @param: customer
     * @description: 分类标准单据体
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/28
     */
    void setCustomerEntryGroupstandard(Map<String, Object> data, DynamicObject customer) {
        Object customerId = customer.get("id");
        String sql = "delete from t_bd_customergroupdetail where fcustomerid=?";
        Object[] ids = {customerId};
        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
        DBRoute dbRoute = new DBRoute("sys");
        boolean execute = DB.execute(dbRoute, sql, ids);
        //获取分类标准数据,并根据该数据进行分类
        List<Map<String, Object>> entryentityDatas = (List<Map<String, Object>>) data.get("entry_groupstandard");
        if (null != entryentityDatas && entryentityDatas.size() > 0) {
            for (Map<String, Object> entryentityData : entryentityDatas) {
                //创建客户分类标准页签对象
                DynamicObject bd_customergroupdetail = BusinessDataServiceHelper.newDynamicObject("bd_customergroupdetail");
                Map<String, Object> groupid = (Map<String, Object>) entryentityData.get("groupid");
                if (null != groupid && groupid.size() > 0) {
                    String groupNumber = (String) groupid.get("number");
                    DynamicObject dynamicObjectType = BusinessDataServiceHelper.loadSingle("zmee_supplier_type", "id,name", new QFilter[]{new QFilter("number", QCP.equals, groupNumber)});//获取苍穹供应商分类编码
                    if (dynamicObjectType != null) {
                        String cqNumber = dynamicObjectType.getString("name");
                        DynamicObject dynamicObjectGroup = BusinessDataServiceHelper.loadSingle("bd_customergroup", "id,number", new QFilter[]{new QFilter("number", QCP.equals, cqNumber)});
                        bd_customergroupdetail.set("group", dynamicObjectGroup);
                    }


                } else {
                    bd_customergroupdetail.set("group", null);
                }

                DynamicObject dystandardId = BusinessDataServiceHelper.loadSingle("bd_customergroupstandard",
                        "number", new QFilter[]{new QFilter("number", "=", "JBFLBZ")});
                //客户表中分类标准单据体没有表名，需要操作另外一张表存
                //获取当前客户数据，用于操作客户分类标准页签这个表
                DynamicObject dynamicObjectWCZD = BusinessDataServiceHelper.loadSingle("bos_org", "id,number", new QFilter[]{new QFilter("zjmi_sapnumber", QCP.equals, "wczd")});
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_customer", "id,number", new QFilter[]{new QFilter("number", QCP.equals, data.get("number"))});
                bd_customergroupdetail.set("standard", dystandardId);
                bd_customergroupdetail.set("customer", customer);
                bd_customergroupdetail.set("createorg", dynamicObjectWCZD);
                SaveServiceHelper.save(new DynamicObject[]{bd_customergroupdetail});
            }
        }

    }

    /**
     * @param: dynamicObject 单据对象
     * @param: org 分配组织
     * @description: TODO 插入视图表。上线数据分配查询
     * @return: void
     * @author: zilongluo
     * @date: 2021/12/14
     */
    void insertCustomerusereg(DynamicObject dynamicObject, DynamicObject org) {
        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
        DBRoute dbRoute = new DBRoute("sys");
        String sql = "insert into T_BD_Customerusereg(fuseorgid,fdataid,fcreateorgid,fadminorgid,fctrlstrategy,fisassign,fassignorgid) values(?,?,100000,100000,2,1,100000)";
        Object[] param1 = {org.get("id"), dynamicObject.get("id")};
        boolean execute = DB.execute(dbRoute, sql, param1);
    }


    /**
     * @param: params
     * @description: MDG行名行号推送接口
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author: zilongluo
     * @date: 2021/6/7
     */
    @Override
    public Map<String, Object> doCustomServiceBankPush(Map<String, Object> params) {
        boolean isSuccess = false;
        HashMap<String, Object> apiResult = new HashMap<>();
        Map<String, Object> data = (Map<String, Object>) params.get("data");
        if (null != data && data.size() > 0) {
            //获取mdg推送的行名行号number
            String number = (String) data.get("number");
            boolean deleteSign = (boolean) data.get("deleteSign");
            if (true == deleteSign) {
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_bebank", "id", new QFilter[]{new QFilter("number", QCP.equals, number)});
                if (dynamicObject != null) {
                    Object id = dynamicObject.get("id");//获取该数据id
                    //禁用该条数据
                    Object[] ids = {id};
                    OperationResult operationResult = OperationServiceHelper.executeOperate("disable", "bd_bebank", ids, OperateOption.create());
                    boolean success = operationResult.isSuccess();
                    if (true == success) {
                        apiResult.put("errorCode", "1001");
                        apiResult.put("success", true);
                        apiResult.put("message", "数据禁用操作成功!");
                        apiResult.put("data", "1001");
                    } else {
                        apiResult.put("errorCode", "1002");
                        apiResult.put("success", false);
                        apiResult.put("message", "数据禁用操作失败!");
                        apiResult.put("data", "1002");

                    }
                } else {
                    apiResult.put("message", "苍穹无此数据，无法禁用!");
                }
            } else {
                //获取行名
                String name = (String) data.get("name");
                //从请求参数获取国家
                Map<String, Object> country = (Map<String, Object>) data.get("country");
                //获取城市文本
                String city = (String) data.get("city");
                //
                String swift_code = (String) data.get("swift_code");
                //获取地址
                String address = (String) data.get("address");
                //获取省份
                Map<String, Object> province = (Map<String, Object>) data.get("province");

                DynamicObject dynamicObjectBank = BusinessDataServiceHelper.loadSingle("bd_bebank", "id,number", new QFilter[]{new QFilter("number", QCP.equals, number)});
                //如果数据存在做更新操作
                if (null != dynamicObjectBank) {
                    Object id = dynamicObjectBank.get("id");
                    DynamicObject dynamicObjectBankUpdate = BusinessDataServiceHelper.loadSingle(id, "bd_bebank");
                    dynamicObjectBankUpdate.set("number", number);
                    dynamicObjectBankUpdate.set("name", name);
                    if (null != country && country.size() > 0) {
                        String numberCountry = (String) country.get("number");
                        if (null != numberCountry) {
                            DynamicObject dynamicObjectCountry = BusinessDataServiceHelper.loadSingle("bd_country", "id,number", new QFilter[]{new QFilter("number", QCP.equals, numberCountry)});
                            dynamicObjectBankUpdate.set("country", dynamicObjectCountry);
                        }
                    } else {
                        dynamicObjectBankUpdate.set("country", null);
                    }
                    if (null != province && province.size() > 0) {
                        String numberProcince = (String) province.get("number");
                        DynamicObject dynamicObjectProcince = BusinessDataServiceHelper.loadSingle("bd_admindivision", "id,number", new QFilter[]{new QFilter("number", QCP.equals, numberProcince)});
                        dynamicObjectBankUpdate.set("province", dynamicObjectProcince);
                    } else {
                        dynamicObjectBankUpdate.set("province", null);
                    }
                    dynamicObjectBankUpdate.set("zmee_city", city);//给城市文本赋值
                    dynamicObjectBankUpdate.set("address", address);//给地址赋值
                    dynamicObjectBankUpdate.set("union_number", number);//给联行号赋值
                    dynamicObjectBankUpdate.set("status", "C");//设置状态字段
                    dynamicObjectBankUpdate.set("enable", "1");//设置可用性
                    SaveServiceHelper.update(new DynamicObject[]{dynamicObjectBankUpdate});//做更新操作
                    isSuccess = true;
                }
                //如果对象不存在做新增操作
                else {
                    DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("bd_bebank");//创建行名行号对象
                    dynamicObject.set("number", number);
                    dynamicObject.set("name", name);
                    if (null != country && country.size() > 0) {
                        String numberCountry = (String) country.get("number");
                        if (null != numberCountry) {
                            DynamicObject dynamicObjectCountry = BusinessDataServiceHelper.loadSingle("bd_country", "id,number", new QFilter[]{new QFilter("number", QCP.equals, numberCountry)});
                            dynamicObject.set("country", dynamicObjectCountry);
                        }
                    }
                    if (null != province && province.size() > 0) {
                        String numberProcince = (String) province.get("number");
                        DynamicObject dynamicObjectProcince = BusinessDataServiceHelper.loadSingle("bd_admindivision", "id,number", new QFilter[]{new QFilter("number", QCP.equals, numberProcince)});
                        dynamicObject.set("province", dynamicObjectProcince);
                    }
                    dynamicObject.set("zmee_city", city);//给城市文本赋值
                    dynamicObject.set("address", address);//给地址赋值
                    dynamicObject.set("union_number", number);//给联行号赋值
                    dynamicObject.set("status", "C");//设置状态字段
                    dynamicObject.set("enable", "1");//设置可用性
                    Object[] save = SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
                    if (null != save && save.length > 0) {
                        isSuccess = true;
                    } else {
                        isSuccess = false;
                    }
                }
                if (isSuccess) {
                    apiResult.put("errorCode", "1001");
                    apiResult.put("success", true);
                    apiResult.put("message", "操作成功!");
                    apiResult.put("data", "1001");
                } else {
                    apiResult.put("errorCode", "1002");
                    apiResult.put("success", false);
                    apiResult.put("message", "操作失败!");
                    apiResult.put("data", "1002");
                }
            }
        }
        return apiResult;
    }

    @Override
    public Map<String, Object> updateSupplier(Map<String, Object> params) {
        boolean isSuccess = false;
        HashMap<String, Object> apiResult = new HashMap<>();
        List<Map<String, Object>> datas = (List) params.get("datas");//获取mdg传输数据
        if (null != datas && datas.size() > 0) {
            for (Map<String, Object> data : datas) {
//                boolean deleteSign = (boolean) data.get("deleteSign");
//                if(deleteSign==true){
//                    String number = (String) data.get("number");
//                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_supplier", "id", new QFilter[]{new QFilter("number", QCP.equals, number)});
//                    if(null!=dynamicObject){
//                        Object id = dynamicObject.get("id");
//                        Object[]ids={id};
//                        OperationResult operationResult = OperationServiceHelper.executeOperate("disable", "bd_supplier", ids, OperateOption.create());
//                        boolean success = operationResult.isSuccess();
//                        if(true==success){
//                            apiResult.put("errorCode","1001");
//                            apiResult.put("success",true);
//                            apiResult.put("message","数据禁用操作成功!");
//                            apiResult.put("data","1001");
//                        }else{
//                            apiResult.put("errorCode","1002");
//                            apiResult.put("success",false);
//                            apiResult.put("message","数据禁用操作失败!");
//                            apiResult.put("data","1002");
//                        }
//                    }else{
//                        apiResult.put("message","苍穹无此数据，无法禁用!");
//                    }
//
//                }


                String number = (String) data.get("number");
                String societycreditcode = (String) data.get("societycreditcode");
                List<Map<String, Object>> createorgs = (List<Map<String, Object>>) data.get("createorg");//创建组织sap编码
                //mdg传的是sap编码，需要转换一次
                List<Map<String, Object>> createorg = new ArrayList<>();
                //将物产集团组织编码存入
                HashMap<String, Object> objectObjectHashMap = new HashMap<>();
                objectObjectHashMap.put("number", "26501");
                createorg.add(objectObjectHashMap);
                if (null != createorgs && createorgs.size() > 0) {
                    for (Map<String, Object> stringObjectMap : createorgs) {
                        String sapnumber = (String) stringObjectMap.get("number");
                        //QFilter bukrsQFilter = new QFilter("zmee_sapnumber", QCP.equals, sapnumber);
                        QFilter bukrsQFilter = new QFilter("zjmi_sapnumber", QCP.equals, sapnumber);
                        DynamicObject[] bos_orgDynamicObjects = BusinessDataServiceHelper.load("bos_org", "id,number,zmee_sapnumber", new QFilter[]{bukrsQFilter});
                        DynamicObject dynamicObject = null;
                        if (!ObjectUtils.isEmpty(bos_orgDynamicObjects)) {
                            for (DynamicObject bos_orgDynamicObject : bos_orgDynamicObjects) {
                                Long id = (Long) bos_orgDynamicObject.get("id");
                                boolean hasAdminBiz = OrgUnitServiceHelper.checkOrgFunction(id, OrgViewType.OrgUnit);
                                if (hasAdminBiz) {
                                    dynamicObject = bos_orgDynamicObject;
                                }
                            }
                        }
//                            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bos_org", "id,number", new QFilter[]{new QFilter("zmee_sapnumber", QCP.equals, sapnumber)});
                        //获取sap编码对应的苍穹的业务组织编码
                        if (!ObjectUtils.isEmpty(dynamicObject)) {
                            String orgNumber = (String) dynamicObject.get("number");
                            Map<String, Object> orgChange = new HashMap<>();
                            orgChange.put("number", orgNumber);
                            createorg.add(orgChange);
                        }
                    }
                }
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_supplier", "id,enable", new QFilter[]{new QFilter("societycreditcode", QCP.equals, societycreditcode)});
                if (null != dynamicObject) {

                    Object idOld = dynamicObject.get("id");
                    DynamicObject dynamicObjectUpdate = BusinessDataServiceHelper.loadSingle(idOld, "bd_supplier");
//                        dynamicObjectUpdate.set("masterid",idOld);
//                        if(StringUtils.equals("0",dynamicObject.getString("enable"))){
//                            try {
//                                OperationResult operationResult = OperationServiceHelper.executeOperate("enable", "bd_supplier", new Object[]{idOld}, OperateOption.create());
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                                logger.info("数据启用失败"+e.getMessage());
//
//                            }
//                        }
                    dynamicObjectUpdate.set("number", number);
                    setBankData(data, dynamicObjectUpdate);//设置银行信息
                    //setentryAccount(data,dynamicObjectUpdate);//设置会计科目分录数据
                    SaveServiceHelper.update(new DynamicObject[]{dynamicObjectUpdate});
//                        setEntryGroupstandard(data,dynamicObjectUpdate);

                    /**
                     * 组织自动分配逻辑
                     * 1.获取供应商表单据中数据中的id
                     * 2.获取业务单元表单据数据中的id
                     * 3.将两张单据id关联存储
                     */
                    //QFilter qFilter = new QFilter("number", QCP.equals, number);
                    QFilter qFilter = new QFilter("societycreditcode", QCP.equals, societycreditcode);
                    DynamicObject dynamicObjects = BusinessDataServiceHelper.loadSingle("bd_supplier", "id", new QFilter[]{qFilter});
                    //供应商库
                    DynamicObject srm_supplier = BusinessDataServiceHelper.loadSingle("srm_supplier", "id", new QFilter[]{qFilter});

                    Object id = dynamicObjects.get("id");
                    //供应商库id
                    Object srmSupplierId = srm_supplier.get("id");

                    DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
                    DBRoute dbRoute = new DBRoute("sys");

                    DBRoute scm = new DBRoute("scm");

                    String deletesql = "delete from t_bd_supplier_u where fdataid=?";

                    //供应商库
                    String srmDeletesql = "delete from t_pur_regsupplier_u where fdataid=?";

                    Object[] paramDelete = {id};

                    Object[] srmDeleteParam = {srmSupplierId};

                    boolean execute1 = DB.execute(dbRoute, deletesql, paramDelete);
                    if (execute1) {

                        boolean execute = DB.execute(scm, srmDeletesql, srmDeleteParam);
                        logger.info("优采云更新关联库" + execute);

                        String supplierusereg = "delete from t_pur_regsupplierusereg where fdataid=?";
                        boolean executereg = DB.execute(scm, supplierusereg, srmDeleteParam);
                        logger.info("优采云更新分配库库" + execute);

                    }
                    String deletesqlreg = "delete from T_BD_Supplierusereg where fdataid=?";
                    boolean executereg = DB.execute(dbRoute, deletesqlreg, paramDelete);

                    for (Map<String, Object> stringObjectMap : createorg) {
                        Object number1 = stringObjectMap.get("number");
                        QFilter qFilter1 = new QFilter("number", QCP.equals, number1);
                        DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle("bos_org", "id", new QFilter[]{qFilter1});
                        Object id1 = dynamicObject1.get("id");

                        //供应商库


                        String sql = "insert into t_bd_supplier_u(fdataid,fuseorgid) values(?,?)";

                        String purSql = "insert into t_pur_regsupplier_u(fdataid,fuseorgid) values(?,?)";
                        Object[] param1 = {id, id1};

                        Object[] purParam = {srmSupplierId, id1};
                        //刷新供应商库数据库20230307
                        boolean executePur = DB.execute(scm, purSql, purParam);


                        boolean execute = DB.execute(dbRoute, sql, param1);
                        insertSupplierusereg(dynamicObjects, dynamicObject1);
                        insertPurSupplierusereg(srm_supplier, dynamicObject1);
                    }
                    isSuccess = true;
                }
//                    else{
//                        DynamicObject supplier = BusinessDataServiceHelper.newDynamicObject("bd_supplier");//创建供应商单据对象
//
//                        setBankData(data,supplier);//设置银行信息
//                        //setentryAccount(data,supplier);//设置会计科目分录
//                        Object[] save = SaveServiceHelper.save(new DynamicObject[]{supplier});
//                        if(null!=save&&save.length>0){
//                            setEntryGroupstandard(data,supplier);
//                            //因生成凭证需要，需要给masterid赋值
//                            DynamicObject dynamicObjectforMaterId = BusinessDataServiceHelper.loadSingle("bd_supplier", "id,number,masterid", new QFilter[]{new QFilter("number", QCP.equals, number)});
//                            Long id = (Long) dynamicObjectforMaterId.get("id");
//                            dynamicObjectforMaterId.set("masterid",id);
//                            SaveServiceHelper.save(new DynamicObject[]{dynamicObjectforMaterId});
//                        }
//                        QFilter qFilter = new QFilter("number", QCP.equals, number);
//                        DynamicObject dynamicObjects = BusinessDataServiceHelper.loadSingle("bd_supplier", "id", new QFilter[]{qFilter});
//                        Object id = dynamicObjects.get("id");
//                        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
//                        DBRoute dbRoute = new DBRoute("sys");
//                        for (Map<String, Object> stringObjectMap : createorg) {
//                            Object number1 = stringObjectMap.get("number");
//                            QFilter qFilter1 = new QFilter("number", QCP.equals, number1);
//                            DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle("bos_org", "id", new QFilter[]{qFilter1});
//                            Object id1 = dynamicObject1.get("id");
//                            String sql = "insert into t_bd_supplier_u(fdataid,fuseorgid) values(?,?)";
//                            Object[] param1={id,id1};
//                            boolean execute = DB.execute(dbRoute, sql, param1);
//                            insertSupplierusereg(dynamicObjects,dynamicObject1);//分配表
//                        }
//                        isSuccess=true;
//                    }
                if (isSuccess) {
                    apiResult.put("errorCode", "1001");
                    apiResult.put("success", true);
                    apiResult.put("message", "操作成功!");
                    apiResult.put("data", "1001");
                } else {
                    apiResult.put("errorCode", "1002");
                    apiResult.put("success", false);
                    apiResult.put("message", "操作失败!");
                    apiResult.put("data", "1002");
                }

            }
        }
        return apiResult;
    }


    /**
     * @param: params
     * @description: 保存mdg供应商信息
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author: zilongluo
     * @date: 2021/6/28
     */
    @Override
    public Map<String, Object> doCustomServiceSupplier(Map<String, Object> params) throws ParseException {
        boolean isSuccess = false;
        HashMap<String, Object> apiResult = new HashMap<>();
        List<Map<String, Object>> datas = (List) params.get("datas");//获取mdg传输数据
        if (null != datas && datas.size() > 0) {
            for (Map<String, Object> data : datas) {
                boolean deleteSign = (boolean) data.get("deleteSign");
                if (deleteSign == true) {
                    String number = (String) data.get("number");
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_supplier", "id", new QFilter[]{new QFilter("number", QCP.equals, number)});
                    if (null != dynamicObject) {
                        Object id = dynamicObject.get("id");
                        Object[] ids = {id};
                        OperationResult operationResult = OperationServiceHelper.executeOperate("disable", "bd_supplier", ids, OperateOption.create());
                        boolean success = operationResult.isSuccess();
                        if (true == success) {
                            apiResult.put("errorCode", "1001");
                            apiResult.put("success", true);
                            apiResult.put("message", "数据禁用操作成功!");
                            apiResult.put("data", "1001");
                        } else {
                            apiResult.put("errorCode", "1002");
                            apiResult.put("success", false);
                            apiResult.put("message", "数据禁用操作失败!");
                            apiResult.put("data", "1002");
                        }
                    } else {
                        apiResult.put("message", "苍穹无此数据，无法禁用!");
                    }

                } else {

                    String number = (String) data.get("number");
                    String societycreditcode = (String) data.get("societycreditcode");
                    List<Map<String, Object>> createorgs = (List<Map<String, Object>>) data.get("createorg");//创建组织sap编码
                    //mdg传的是sap编码，需要转换一次
                    List<Map<String, Object>> createorg = new ArrayList<>();
                    //将物产集团组织编码存入
                    HashMap<String, Object> objectObjectHashMap = new HashMap<>();
//                    objectObjectHashMap.put("number","26501");
//                    createorg.add(objectObjectHashMap);
                    if (null != createorgs && createorgs.size() > 0) {
                        for (Map<String, Object> stringObjectMap : createorgs) {
                            String sapnumber = (String) stringObjectMap.get("number");
                            QFilter bukrsQFilter = new QFilter("zmee_sapnumber", QCP.equals, sapnumber);
                            //QFilter bukrsQFilter = new QFilter("zjmi_sapnumber", QCP.equals, sapnumber);
                            DynamicObject[] bos_orgDynamicObjects = BusinessDataServiceHelper.load("bos_org", "id,number,zmee_sapnumber", new QFilter[]{bukrsQFilter});
                            DynamicObject dynamicObject = null;
                            if (!ObjectUtils.isEmpty(bos_orgDynamicObjects)) {
                                for (DynamicObject bos_orgDynamicObject : bos_orgDynamicObjects) {
                                    Long id = (Long) bos_orgDynamicObject.get("id");
                                    boolean hasAdminBiz = OrgUnitServiceHelper.checkOrgFunction(id, OrgViewType.OrgUnit);
                                    if (hasAdminBiz) {
                                        dynamicObject = bos_orgDynamicObject;
                                    }
                                }
                            }
//                            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bos_org", "id,number", new QFilter[]{new QFilter("zmee_sapnumber", QCP.equals, sapnumber)});
                            //获取sap编码对应的苍穹的业务组织编码
                            if (!ObjectUtils.isEmpty(dynamicObject)) {
                                String orgNumber = (String) dynamicObject.get("number");
                                Map<String, Object> orgChange = new HashMap<>();
                                orgChange.put("number", orgNumber);
                                createorg.add(orgChange);
                            }
                        }
                    }
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_supplier", "id,enable", new QFilter[]{new QFilter("societycreditcode", QCP.equals, societycreditcode)});
                    if (null != dynamicObject) {

                        Object idOld = dynamicObject.get("id");
                        DynamicObject dynamicObjectUpdate = BusinessDataServiceHelper.loadSingle(idOld, "bd_supplier");
                        dynamicObjectUpdate.set("masterid", idOld);
                        if (StringUtils.equals("0", dynamicObject.getString("enable"))) {
                            try {
                                OperationResult operationResult = OperationServiceHelper.executeOperate("enable", "bd_supplier", new Object[]{idOld}, OperateOption.create());
                            } catch (Exception e) {
                                e.printStackTrace();
                                logger.info("数据启用失败" + e.getMessage());

                            }
                        }
                        setSupplierHeader(data, dynamicObjectUpdate);//设置单据头
                        setBusinessData(data, dynamicObjectUpdate);//设置工商信息单据体
                        setBankData(data, dynamicObjectUpdate);//设置银行信息
                        //setentryAccount(data,dynamicObjectUpdate);//设置会计科目分录数据
                        SaveServiceHelper.update(new DynamicObject[]{dynamicObjectUpdate});
                        setEntryGroupstandard(data, dynamicObjectUpdate);

                        /**
                         * 组织自动分配逻辑
                         * 1.获取供应商表单据中数据中的id
                         * 2.获取业务单元表单据数据中的id
                         * 3.将两张单据id关联存储
                         */
                        QFilter qFilter = new QFilter("societycreditcode", QCP.equals, societycreditcode);
                        DynamicObject dynamicObjects = BusinessDataServiceHelper.loadSingle("bd_supplier", "id", new QFilter[]{qFilter});
                        //供应商库
                        DynamicObject srm_supplier = BusinessDataServiceHelper.loadSingle("srm_supplier", "id", new QFilter[]{qFilter});
                        Object id = dynamicObjects.get("id");
                        //供应商库id
                        Object srmSupplierId = null;
                        if (!ObjectUtils.isEmpty(srm_supplier)) {
                            srmSupplierId = srm_supplier.get("id");
                        }


                        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
                        DBRoute dbRoute = new DBRoute("sys");

                        DBRoute scm = new DBRoute("scm");

                        String deletesql = "delete from t_bd_supplier_u where fdataid=? AND fuseorgid != '100000'";

                        //供应商库
                        String srmDeletesql = "delete from t_pur_regsupplier_u where fdataid=? AND fuseorgid != '100000'";

                        Object[] paramDelete = {id};

                        Object[] srmDeleteParam = {srmSupplierId};

                        boolean execute1 = DB.execute(dbRoute, deletesql, paramDelete);
                        if (execute1 && srmSupplierId != null) {

                            boolean execute = DB.execute(scm, srmDeletesql, srmDeleteParam);
                            logger.info("优采云更新关联库" + execute);

                            String supplierusereg = "delete from t_pur_regsupplierusereg where fdataid=? AND fuseorgid !='100000'";
                            boolean executereg = DB.execute(scm, supplierusereg, srmDeleteParam);
                            logger.info("优采云更新分配库库" + execute);

                        }
                        String deletesqlreg = "delete from T_BD_Supplierusereg where fdataid=? AND fuseorgid !='100000'";
                        boolean executereg = DB.execute(dbRoute, deletesqlreg, paramDelete);

                        for (Map<String, Object> stringObjectMap : createorg) {
                            Object number1 = stringObjectMap.get("number");
                            QFilter qFilter1 = new QFilter("number", QCP.equals, number1);
                            DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle("bos_org", "id", new QFilter[]{qFilter1});
                            Object id1 = dynamicObject1.get("id");

                            //供应商库


                            String sql = "insert into t_bd_supplier_u(fdataid,fuseorgid) values(?,?)";

                            String purSql = "insert into t_pur_regsupplier_u(fdataid,fuseorgid) values(?,?)";
                            Object[] param1 = {id, id1};

                            Object[] purParam = {srmSupplierId, id1};
                            //刷新供应商库数据库20230307
                            if (srmSupplierId != null) {
                                boolean executePur = DB.execute(scm, purSql, purParam);
                                insertPurSupplierusereg(srm_supplier, dynamicObject1);
                            }


                            boolean execute = DB.execute(dbRoute, sql, param1);
                            insertSupplierusereg(dynamicObjects, dynamicObject1);

                        }
                        isSuccess = true;
                    } else {
                        isSuccess = false;
//                        DynamicObject supplier = BusinessDataServiceHelper.newDynamicObject("bd_supplier");//创建供应商单据对象
//                        setSupplierHeader(data,supplier);//设置单据头
//                        setBusinessData(data,supplier);//设置工商信息单据体
//                        setBankData(data,supplier);//设置银行信息
//                        //setentryAccount(data,supplier);//设置会计科目分录
//                        Object[] save = SaveServiceHelper.save(new DynamicObject[]{supplier});
//                        if(null!=save&&save.length>0){
//                            setEntryGroupstandard(data,supplier);
//                            //因生成凭证需要，需要给masterid赋值
//                            DynamicObject dynamicObjectforMaterId = BusinessDataServiceHelper.loadSingle("bd_supplier", "id,number,masterid", new QFilter[]{new QFilter("number", QCP.equals, number)});
//                            Long id = (Long) dynamicObjectforMaterId.get("id");
//                            dynamicObjectforMaterId.set("masterid",id);
//                            SaveServiceHelper.save(new DynamicObject[]{dynamicObjectforMaterId});
//                        }
//                        QFilter qFilter = new QFilter("number", QCP.equals, number);
//                        DynamicObject dynamicObjects = BusinessDataServiceHelper.loadSingle("bd_supplier", "id", new QFilter[]{qFilter});
//                        Object id = dynamicObjects.get("id");
//                        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
//                        DBRoute dbRoute = new DBRoute("sys");
//                        for (Map<String, Object> stringObjectMap : createorg) {
//                            Object number1 = stringObjectMap.get("number");
//                            QFilter qFilter1 = new QFilter("number", QCP.equals, number1);
//                            DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle("bos_org", "id", new QFilter[]{qFilter1});
//                            Object id1 = dynamicObject1.get("id");
//                            String sql = "insert into t_bd_supplier_u(fdataid,fuseorgid) values(?,?)";
//                            Object[] param1={id,id1};
//                            boolean execute = DB.execute(dbRoute, sql, param1);
//                            insertSupplierusereg(dynamicObjects,dynamicObject1);//分配表
//                        }
//                        isSuccess=true;
                    }
                    if (isSuccess) {
                        apiResult.put("errorCode", "1001");
                        apiResult.put("success", true);
                        apiResult.put("message", "操作成功!");
                        apiResult.put("data", "1001");
                    } else {
                        apiResult.put("errorCode", "1002");
                        apiResult.put("success", false);
                        apiResult.put("message", "操作失败!");
                        apiResult.put("data", "1002");
                    }
                }
            }
        }
        return apiResult;
    }

    /**
     * @param: dynamicObject 单据对象
     * @param: org 分配组织
     * @description: TODO 插入视图表。上线数据分配查询
     * @return: void
     * @author: zilongluo
     * @date: 2021/12/14
     */
    void insertSupplierusereg(DynamicObject dynamicObject, DynamicObject org) {
        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
        DBRoute dbRoute = new DBRoute("sys");
        String sql = "insert into T_BD_Supplierusereg(fuseorgid,fdataid,fcreateorgid,fadminorgid,fctrlstrategy,fisassign,fassignorgid) values(?,?,100000,100000,2,1,100000)";
        Object[] param1 = {org.get("id"), dynamicObject.get("id")};
        boolean execute = DB.execute(dbRoute, sql, param1);
    }

    /**
     * @param: dynamicObject 单据对象
     * @param: org 分配组织
     * @description: TODO 插入视图表。上线数据分配查询
     * @return: void
     * @author: zilongluo
     * @date: 2021/12/14
     */
    void insertPurSupplierusereg(DynamicObject dynamicObject, DynamicObject org) {
        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
        DBRoute dbRoute = new DBRoute("scm");
        String sql = "insert into t_pur_regsupplierusereg(fuseorgid,fdataid,fcreateorgid,fadminorgid,fctrlstrategy,fisassign,fassignorgid) values(?,?,100000,100000,2,1,100000)";
        Object[] param1 = {org.get("id"), dynamicObject.get("id")};
        boolean execute = DB.execute(dbRoute, sql, param1);
    }


    /**
     * @param: params
     * @description: 设置供应商单据头信息
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/27
     */
    void setSupplierHeader(Map<String, Object> data, DynamicObject supplier) {
        supplier.set("number", data.get("number"));//设置编码值
        //supplier.set("zjmi_iswcyy",true);//设置编码值
        supplier.set("name", data.get("name"));//名称
        //supplier.set("zjmi_supplier_type",data.get("zjmi_supplier_type"));//获取供应商分类字段
        //supplier.set("zjmi_employee_number",data.get("zjmi_employee_number"));//员工编号
        //supplier.set("zjmi_business_partber_ide",data.get("zjmi_business_partber_ide"));//统一社会信用代码
        //隐藏的字段
        supplier.set("enable", "1");//使用状态:0禁用 1 可用
        supplier.set("status", "C");//数据状态:A:暂存 B:已提交 C:已审核
        supplier.set("ctrlstrategy", "2");//控制策略:1:逐级分配 2:自由分配 5:全局共享 6:管控范围内共享 7:私有
        //业务职能
        supplier.set("bizfunction", "1,2,3,4");
//        DynamicObject dyorg = BusinessDataServiceHelper.loadSingle("bos_org", "id,number,name",
//                new QFilter[]{new QFilter("zjmi_sapnumber","=","01K0")});

        DynamicObject[] bos_orgDynamicObjects = BusinessDataServiceHelper.load("bos_org", "id,number,zmee_sapnumber", new QFilter[]{new QFilter("number", "=", "26501")});
        DynamicObject dynamicObject = new DynamicObject();
        if (!ObjectUtils.isEmpty(bos_orgDynamicObjects)) {
            for (DynamicObject bos_orgDynamicObject : bos_orgDynamicObjects) {
                Long id = (Long) bos_orgDynamicObject.get("id");
                boolean hasAdminBiz = OrgUnitServiceHelper.checkOrgFunction(id, OrgViewType.OrgUnit);
                if (hasAdminBiz) {
                    dynamicObject = bos_orgDynamicObject;
                    supplier.set("createorg", dynamicObject);//所属组织
                }
            }
        }


        DynamicObject supplier_status = BusinessDataServiceHelper.loadSingle("bd_supplierstatus", "id,number,name",
                new QFilter[]{new QFilter("number", "=", "ZCGYS")});
        supplier.set("supplier_status", supplier_status);//供应商状态
        //supplier.set("zjmi_search_item",data.get("zjmi_search_item"));
    }

    /**
     * @param: data
     * @param: supplier
     * @description: 设置工商信息单据体信息
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/27
     */
    void setBusinessData(Map<String, Object> data, DynamicObject supplier) throws ParseException {
        Map<String, String> country = (Map<String, String>) data.get("country");
        String countrynumber = country.get("number");//国家/地区编号
        DynamicObject countryDy = BusinessDataServiceHelper.loadSingle("bd_country",
                "number", new QFilter[]{new QFilter("number", "=", countrynumber)});
        supplier.set("country", countryDy);//
        supplier.set("societycreditcode", data.get("societycreditcode"));
        supplier.set("tx_register_no", data.get("societycreditcode"));
        supplier.set("artificialperson", data.get("artificialperson"));//法人代表
        supplier.set("regcapital", data.get("regcapital"));//注册资本（万）
        String establishdate = (String) data.get("establishdate");//成立日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (null != establishdate) {
            Date date = simpleDateFormat.parse(establishdate);
            supplier.set("establishdate", date);//成立日期
        } else {
            supplier.set("establishdate", null);
        }
//        if(null!=data.get("zjmi_supplier_type")&&data.get("zjmi_supplier_type").equals("0")){
//            supplier.set("idno",data.get("societycreditcode"));//当供应商类型为个人时，需要填写身份证号码
//            supplier.set("type","4");//伙伴类型
//        }else {
//            supplier.set("type","M01");//伙伴类型
//        }
        supplier.set("bizpartner_phone", data.get("bizpartner_phone"));//联系电话
        supplier.set("bizpartner_fax", data.get("bizpartner_fax"));//传真
        supplier.set("postal_code", data.get("postal_code"));//电子邮箱
        Map<String, String> admindivision = (Map<String, String>) data.get("admindivision");
        if (null != admindivision && admindivision.size() > 0) {
            String admindivisionnumber = admindivision.get("number");//国家/地区编号
            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("bd_admindivision", "id", new QFilter[]{new QFilter("number", QCP.equals, admindivisionnumber)});
            if (null != dynamicObject) {
                supplier.set("admindivision", dynamicObject.get("id"));
            }
        } else {
            supplier.set("admindivision", null);
        }
        supplier.set("bizpartner_address", data.get("bizpartner_address"));//详细地址
    }


    /**
     * @param: data
     * @param: supplier
     * @description: 设置银行信息单据体
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/27
     */
    void setBankData(Map<String, Object> data, DynamicObject supplier) {
        List<Map<String, Object>> entryBankDatas = (List<Map<String, Object>>) data.get("entry_bank");
        DynamicObjectCollection entry_banks = supplier.getDynamicObjectCollection("entry_bank");
        if (null != entry_banks) {
            entry_banks.clear();
        }
        if (null != entryBankDatas && entryBankDatas.size() > 0) {
            for (Map<String, Object> entryBankData : entryBankDatas) {
                DynamicObject dynamicObjectBank = entry_banks.addNew();
                dynamicObjectBank.set("bankaccount", entryBankData.get("bankaccount"));//银行账号
                //设置币别字段
                QFilter qFilter = new QFilter("number", QCP.equals, "CNY");//设置币别过滤条件
                DynamicObject dynamicObjectCurrncy = BusinessDataServiceHelper.loadSingle("bd_currency", "id,number", new QFilter[]{qFilter});
                dynamicObjectBank.set("currency", dynamicObjectCurrncy);//设置币别为人民币
                dynamicObjectBank.set("accountname", entryBankData.get("accountname"));//账户名称
                //开户银行
                Map<String, String> bank = (Map<String, String>) entryBankData.get("bank");
                dynamicObjectBank.set("isdefault_bank", entryBankData.get("isdefault_bank"));
                if (null != bank && bank.size() > 0) {
                    String banknumber = bank.get("number");//开户银行编号
                    DynamicObject dybank = BusinessDataServiceHelper.loadSingle("bd_bebank",
                            "number", new QFilter[]{new QFilter("number", "=", banknumber)});
                    dynamicObjectBank.set("bank", dybank);
                }
            }
        }
        SaveServiceHelper.save(new DynamicObject[]{supplier});
    }


    /**
     * @param: data
     * @param: supplier
     * @description: 保存分类分录单据体
     * @return: void
     * @author: zilongluo
     * @date: 2021/6/28
     */
    void setEntryGroupstandard(Map<String, Object> data, DynamicObject supplier) {
        Object supplierId = supplier.get("id");
        String sql = "delete from t_bd_suppliergroupdetail where fsupplierid=?";
        Object[] ids = {supplierId};
        DynamicObjectResultSetCallback<Object> callback = new DynamicObjectResultSetCallback<>();
        DBRoute dbRoute = new DBRoute("sys");
        boolean execute = DB.execute(dbRoute, sql, ids);
        //获取分类标准数据,并根据该数据进行分类
        List<Map<String, Object>> suEntryentityDatas = (List<Map<String, Object>>) data.get("entry_groupstandard");
        if (null != suEntryentityDatas && suEntryentityDatas.size() > 0) {
            for (Map<String, Object> entryentityData : suEntryentityDatas) {
                //创建供应商分类标准页签对象
                DynamicObject bd_suppliergroupdetail = BusinessDataServiceHelper.newDynamicObject("bd_suppliergroupdetail");
                Map<String, Object> groupid = (Map<String, Object>) entryentityData.get("groupid");
                if (null != groupid && groupid.size() > 0) {
                    String groupNumber = (String) groupid.get("number");
                    DynamicObject dynamicObjectType = BusinessDataServiceHelper.loadSingle("zmee_supplier_type", "id,name", new QFilter[]{new QFilter("number", QCP.equals, groupNumber)});//获取苍穹供应商分类编码
                    //DynamicObject dynamicObjectType = BusinessDataServiceHelper.loadSingle("zjmi_supplier_type", "id,name", new QFilter[]{new QFilter("number", QCP.equals, groupNumber)});//获取苍穹供应商分类编码
                    if (dynamicObjectType != null) {
                        String cqNumber = dynamicObjectType.getString("name");//
                        DynamicObject dynamicObjectGroup = BusinessDataServiceHelper.loadSingle("bd_suppliergroup", "id,number", new QFilter[]{new QFilter("number", QCP.equals, cqNumber)});
                        bd_suppliergroupdetail.set("group", dynamicObjectGroup);
                    }
                } else {
                    bd_suppliergroupdetail.set("group", null);
                }
                DynamicObject dystandardId = BusinessDataServiceHelper.loadSingle("bd_suppliergroupstandard",
                        "number", new QFilter[]{new QFilter("number", "=", "JBFLBZ")});
                //供应商表中分类标准单据体没有表名，需要操作另外一张表存
                //获取当前供应商数据，用于操作客户分类标准页签这个表
//                    DynamicObject dynamicObjectWCZD = BusinessDataServiceHelper.loadSingle("bos_org", "id,number", new QFilter[]{new QFilter("zjmi_sapnumber", QCP.equals, "wczd")});
                DynamicObject dynamicObjectWCZD = BusinessDataServiceHelper.loadSingle("bos_org", "id,number", new QFilter[]{new QFilter("number", QCP.equals, "26501")});
                DynamicObject dynamicObjectSup = BusinessDataServiceHelper.loadSingle("bd_supplier", "id,number", new QFilter[]{new QFilter("id", QCP.equals, supplier.get("id"))});
                bd_suppliergroupdetail.set("standard", dystandardId);
                bd_suppliergroupdetail.set("supplier", dynamicObjectSup);
                bd_suppliergroupdetail.set("createorg", dynamicObjectWCZD);
                SaveServiceHelper.save(new DynamicObject[]{bd_suppliergroupdetail});
            }
        }
    }


    /**
     * @param: data
     * @param: supplier
     * @description: 新增会计科目分录
     * @return: void
     * @author: zilongluo
     * @date: 2021/7/21
     */
    void setentryAccount(Map<String, Object> data, DynamicObject supplier) {
        DynamicObjectCollection zjmi_entry_account = supplier.getDynamicObjectCollection("zjmi_entry_account");
        //更新前先将已有的单据体清除
        if (null != zjmi_entry_account) {
            zjmi_entry_account.clear();
        }
        //获取开票信息
        List<Map<String, Object>> zjmi_entry_accounts = (List<Map<String, Object>>) data.get("zjmi_entry_account");
        if (null != zjmi_entry_accounts && zjmi_entry_accounts.size() > 0) {
            for (Map<String, Object> zjmiEntryTicket : zjmi_entry_accounts) {
                //在开票信息单据体中新增一行
                DynamicObject dynamicObjectTicketRow = zjmi_entry_account.addNew();
                Map<String, Object> zjmi_ticket_org = (Map<String, Object>) zjmiEntryTicket.get("zjmi_org");
                if (null != zjmi_ticket_org && zjmi_ticket_org.size() > 0) {
                    String orgnumber = (String) zjmi_ticket_org.get("number");
                    QFilter bukrsQFilter = new QFilter("zjmi_sapnumber", QCP.equals, orgnumber);
                    DynamicObject[] bos_orgDynamicObjects = BusinessDataServiceHelper.load("bos_org", "id,number,zjmi_sapnumber", new QFilter[]{bukrsQFilter});
                    DynamicObject dynamicObject = new DynamicObject();
                    if (!ObjectUtils.isEmpty(bos_orgDynamicObjects)) {
                        for (DynamicObject bos_orgDynamicObject : bos_orgDynamicObjects) {
                            Long id = (Long) bos_orgDynamicObject.get("id");
                            boolean hasAdminBiz = OrgUnitServiceHelper.checkOrgFunction(id, OrgViewType.OrgUnit);
                            if (hasAdminBiz) {
                                dynamicObject = bos_orgDynamicObject;
                                dynamicObjectTicketRow.set("zjmi_org", dynamicObject);
                            }
                        }
                    }
//
//
//
//                    DynamicObject dynamicObjectTicket = BusinessDataServiceHelper.loadSingle("bos_org", "id,number", new QFilter[]{new QFilter("zjmi_sapnumber", QCP.equals, orgnumber)});
//                    dynamicObjectTicketRow.set("zjmi_org",dynamicObjectTicket);
                }
                String zjmi_mdg_ticket = (String) zjmiEntryTicket.get("zjmi_account");
                dynamicObjectTicketRow.set("zjmi_account", zjmi_mdg_ticket);
            }
        }
        SaveServiceHelper.save(new DynamicObject[]{supplier});
    }

    /**
     * @param: callback  接口返回值
     * @param: b 接口调用成功
     * @param: 返回撤回失败原因
     * @description: 每刻撤回接口返回值
     * @return: java.util.Map 接口返回值
     * @author: zilongluo
     * @date: 2022/3/21
     */
    Map returnData(Map<String, Object> callback, boolean b, String errorMessage) {
        if (b) {
            callback.put("code", "1001");
            callback.put("success", true);
            callback.put("message", "操作成功!");
        } else if (!b) {
            callback.put("code", "1002");
            callback.put("success", false);
            callback.put("message", errorMessage);
        }
        return callback;
    }

    /**
     * @param: orgNunber
     * @description: 获取上级组织为拆入组织的所有业务单元
     * @return: java.util.ArrayList<java.lang.Long>
     * @author: zilongluo
     * @date: 2022/7/6
     */
    ArrayList<Long> queryOrgStructure(String orgNunber) {
        ArrayList<Long> ids = new ArrayList<>();
        //根据所处组织查询当前组织架构中上级为该组织的组织
        //
        DynamicObject query = QueryServiceHelper.queryOne("bos_org", "id", new QFilter[]{new QFilter("number", QCP.equals, orgNunber)});
        QFilter filter = new QFilter("parent", QCP.equals, query.getLong("id"));
        DynamicObject dynamicObject = QueryServiceHelper.queryOne("bos_org_viewschema", "id", new QFilter[]{new QFilter("number", QCP.equals, "15")});
        filter.and(new QFilter("view", QCP.equals, dynamicObject.getLong("id")));
        DynamicObjectCollection structures = QueryServiceHelper.query("bos_org_structure", "number,org", new QFilter[]{filter});
        if (!ObjectUtils.isEmpty(structures)) {
            for (DynamicObject structure : structures) {
                ids.add(structure.getLong("id"));
            }
            return ids;
        }
        return null;
    }
}